package encryption;

import java.security.MessageDigest;
import java.util.Arrays;


public class Process {
    private final static String VERSOIN_VALUE = "1.0"; /* */
    private final static String SYSNAME_VALUE = "Ministry"; /* */

     private final static int SIGN_MAXLEN = 8; /**/

    private final static int VERSOIN_MAXLEN = 3; /* */

  
    private final static int SYSNAME_MAXLEN = 8; /**/

    private final static int RESERVE_MAXLEN = 64; /**/

	public static int ERRCODE_SHENG_SIGN_FAIL= 8020;
	public static int ERRCODE_SHENG_VERIFY_FAIL=8021;
	public static int ERRCODE_SHENG_ENCRYPT_FAIL=8022;

    private final static int BUSHENG_HEADLEN = (VERSOIN_MAXLEN + SYSNAME_MAXLEN + SIGN_MAXLEN + RESERVE_MAXLEN); /**/

  

    private final static byte FILL_HEADER_BYTE = ' '; /* */

 
    public int parseData(byte[] requestData, Info transDataInfo) {
        //System.out.println("[parseData]begin..................");
        byte[] decryptData = null;
        int errCode = 0;
        // parse private protocol header
        String version = new String(requestData, 0, VERSOIN_MAXLEN).trim();
        transDataInfo.setVersion(version);
        String systemName = new String(requestData, VERSOIN_MAXLEN, SYSNAME_MAXLEN).trim();
        transDataInfo.setSystemName(systemName);
        String fromSignLen = new String(requestData, VERSOIN_MAXLEN + SYSNAME_MAXLEN, SIGN_MAXLEN).trim();
        int iFromSignLen = Integer.parseInt(fromSignLen);
        
        byte bfromSignData[] = Arrays.copyOfRange(requestData, BUSHENG_HEADLEN, BUSHENG_HEADLEN + iFromSignLen);

      
        int requestDataLen = requestData.length;
      
        int iTransEncrypteLen = requestDataLen - (BUSHENG_HEADLEN + iFromSignLen);

        try {
            // copy encryped data
            byte[] transData = Arrays.copyOfRange(requestData, BUSHENG_HEADLEN + iFromSignLen, BUSHENG_HEADLEN
                    + iFromSignLen + iTransEncrypteLen);
          
            decryptData = transData;
            int decryptLen = iTransEncrypteLen;
            // End......���Ĵ���

            transDataInfo.setBody(decryptData);
           
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(decryptData, 0, decryptLen);
            byte[] calSignData = mdTemp.digest();
           
            int calSignDataLen = calSignData.length;
            if (calSignDataLen != iFromSignLen) {
                errCode = ERRCODE_SHENG_VERIFY_FAIL;
                throw new Exception("[Verify Sign Len]calSignDataLen[" + calSignDataLen + "]!=iFromSignLen["
                        + +iFromSignLen + "]");
            }
            for (int i = 0; i < iFromSignLen; i++) {
                if (calSignData[i] != bfromSignData[i]) {
                    errCode = ERRCODE_SHENG_VERIFY_FAIL;
                    throw new Exception("[Verify Sign failed]Location index[" + i + "]");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //System.out.println("[parseData]end..................");
        return errCode;
    }

    private byte[] getFixLenByte(byte[] data, int fixLen, byte fillByte) {
        byte bData[] = new byte[fixLen];
        int srcLen = data.length;
        if (srcLen > fixLen)
            srcLen = fixLen;
        System.arraycopy(data, 0, bData, 0, srcLen);
        for (int i = srcLen; i < fixLen; i++)
            bData[i] = fillByte;
        return bData;
    }

    public int packData(byte[] responseData, Info transDataInfo) {
        //System.out.println("[packData]begin..................");
        byte[] calSignData;
        byte[] packData;
        int errCode = 0;
        try {
            // Sign source data
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(responseData);
            calSignData = mdTemp.digest();
            //System.out.println("[Caluate Sign data]");
           printHex(calSignData);
        } catch (Exception e) {
            errCode = ERRCODE_SHENG_SIGN_FAIL;
            e.printStackTrace();
            //System.out.println("[packData]end..................");
            return errCode;
        }
        try {
            byte[] encryptData;
            /*
             * ���Ĵ��� //encrypt data; int len = responseData.length; int kk =
             * len%8; //file zero byte if(kk!=0){ len += 8- kk; responseData =
             * getFixLenByte(responseData, len, FILL_CRYPT_BYTE); }
             * System.out.println("[before encrypt data]");
             * DesUtil.printHex(responseData); //encrypt data; encryptData =
             * DesUtil.encrypt(responseData, CRYPT_KEY.getBytes());
             * System.out.println("[After encrypt data]");
             * DesUtil.printHex(encryptData);���Ĵ���
             */
            // Begin....���Ĵ���
            encryptData = responseData;
            // End......���Ĵ���

            int iSignLen = calSignData.length;
            int iEncryptLen = encryptData.length;
            // pack data
            packData = new byte[BUSHENG_HEADLEN + iSignLen + iEncryptLen];
            // pack header
            System.arraycopy(VERSOIN_VALUE.getBytes(), 0, packData, 0, VERSOIN_MAXLEN);
            System.arraycopy(SYSNAME_VALUE.getBytes(), 0, packData, VERSOIN_MAXLEN, SYSNAME_MAXLEN);
            byte bSignLen[] = getFixLenByte(String.valueOf(iSignLen).getBytes(), SIGN_MAXLEN, FILL_HEADER_BYTE);
            System.arraycopy(bSignLen, 0, packData, VERSOIN_MAXLEN + SYSNAME_MAXLEN, SIGN_MAXLEN);
            // pack sign data
            System.arraycopy(calSignData, 0, packData, BUSHENG_HEADLEN, iSignLen);
            // pack encrypt data
            System.arraycopy(encryptData, 0, packData, BUSHENG_HEADLEN + iSignLen, iEncryptLen);

            transDataInfo.setBody(packData);

        } catch (Exception e) {
            errCode = ERRCODE_SHENG_ENCRYPT_FAIL;
            e.printStackTrace();

        }
        //System.out.println("[packData]end..................");
        return errCode;
    }
	  public static String printHex(byte [] buffer){  
	        String h = "";  
	          
	        for(int i = 0; i < buffer.length; i++){  
	            String temp = Integer.toHexString(buffer[i] & 0xFF);  
	            if(temp.length() == 1){  
	                temp = "0" + temp;  
	            }  
	            h = h + " "+ temp;  
	        }  
	          System.out.println(h);
	        return h;  
	          
	    }  

}
